package cn.wen233.srpc.core.spring.server.autoconfigure;

import cn.wen233.srpc.core.server.Server;
import cn.wen233.srpc.core.server.impl.NativeSocketServer;
import cn.wen233.srpc.core.service.SunglowServiceScanHandler;
import cn.wen233.srpc.core.service.spi.SPIContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;

import java.util.List;

/**
 * 暴露端口 用于接收请求
 *
 * @author wenei
 * @date 2021-12-26 14:14
 */
public class Exporter implements ApplicationRunner, Ordered {

    private static final Logger log = LoggerFactory.getLogger(Exporter.class);

    private Integer port;

    private List<String> scans;

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public static Logger getLog() {
        return log;
    }

    public List<String> getScans() {
        return scans;
    }

    public void setScans(List<String> scans) {
        this.scans = scans;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("SunglowRPC 启用监听端口:{}", port);
        for (String path : scans) {
            SunglowServiceScanHandler.handle(path, true);
        }
        Server server = SPIContext.loadSingleService(Server.class);
        server.start(port);
    }

    @Override
    public int getOrder() {
        return LOWEST_PRECEDENCE;
    }
}
